package com.ysd.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.entity.Users;
import com.ysd.entity.UsersLogin;
import com.ysd.service.UsersService;
import com.ysd.util.Result;

@RestController
public class UsersController {
	@Autowired
	private UsersService uservice;

	/*
	 * @RequestMapping("getUsers") public Object getUsers(Users user, HttpSession
	 * session) { Users u = us.getUname(user.getLoginName()); if (u != null) { if
	 * (u.getPassword().equals(user.getPassword())) { session.setAttribute("user",
	 * u); return Result.toClient("0", "��¼�ɹ�", null, u); } else { return
	 * Result.toClient("1", "�˺Ż��������"); } } else { return Result.toClient("1",
	 * "�˺Ż��������"); }
	 * 
	 * }
	 */
	@RequestMapping("getUsers")
	public Object getUsers(UsersLogin us, HttpSession session) {
		UsersLogin ul = uservice.FindUsers(us);
		UsersLogin u = uservice.allgetuser(us);
		int l = ul.getPsdWrongTime();
		if (ul != null) {
			if (ul.getPassword().equals(us.getPassword())) {
				System.out.println(ul);
				session.setAttribute("user", ul);
				session.setAttribute("name", ul.getNames());
				uservice.upLastLoginTime(ul);
				return Result.toClient("0", "��¼�ɹ�", null, u);
			} else {
				if (l < 5) {
					uservice.upPsdWrongTime(ul);
					return Result.toClient("2", "�˺Ż��������");
				} else {
					uservice.upLock(ul);
					return Result.toClient("3", "�ѱ�����");
				}
			}
		} else {
			return Result.toClient("1", "�˺Ż��������!");
		}

	}

	@RequestMapping("Allgetuser")
	public Object Allgetuser(UsersLogin us, HttpSession session) {
		UsersLogin u = uservice.allgetuser(us);
		if (u != null) {
			session.setAttribute("user", u);
			return Result.toClient("0", "1", null, u);
		}
		return Result.toClient("1", "2");
	}

	@RequestMapping("modifyPassword")
	public Object modifyPassword(Users users) {
		int n = uservice.modifyPassword(users);
		if (n > 0) {
			return Result.toClient("0", "修改成功");
		}
		return Result.toClient("1", "修改失败");
	}

	@RequestMapping("selPsd")
	public Object selPsd(Users users) {
		Users n = uservice.findPsdById(users);
		if (n != null) {
			return Result.toClient("0", "查询成功", null, n);
		}
		return Result.toClient("1", "查询失败");
	}

	// 用于退出页面
	@RequestMapping("tologout")
	public Object tologout(HttpSession session) {
		session.removeAttribute("name");
		return "session已清空";
	}
}
